using System;
using UnityEngine;

[RequireComponent(typeof(Camera))]
public class RenderTextureCamera : MonoBehaviour
{
	private void Awake()
	{
		if (this.useFullScreen)
		{
			Resolution currentResolution = Screen.currentResolution;
			this.width = currentResolution.width;
			this.height = currentResolution.height;
		}
		base.GetComponent<Camera>().enabled = false;
	}

	public Texture2D BuildTexture2D()
	{
		RenderTexture temporary = RenderTexture.GetTemporary(this.width, this.height, 16);
		base.GetComponent<Camera>().targetTexture = temporary;
		RenderTexture active = RenderTexture.active;
		RenderTexture.active = temporary;
		base.GetComponent<Camera>().Render();
		Texture2D texture2D = new Texture2D(this.width, this.height, TextureFormat.ARGB32, this.useMipMap);
		texture2D.ReadPixels(new Rect(0f, 0f, (float)this.width, (float)this.height), 0, 0);
		texture2D.Apply();
		RenderTexture.active = active;
		RenderTexture.ReleaseTemporary(temporary);
		return texture2D;
	}

	public void BuildTexture2D(ref Texture2D texture2d)
	{
		RenderTexture temporary = RenderTexture.GetTemporary(this.width, this.height, 16);
		base.GetComponent<Camera>().targetTexture = temporary;
		RenderTexture active = RenderTexture.active;
		RenderTexture.active = temporary;
		base.GetComponent<Camera>().Render();
		texture2d.ReadPixels(new Rect(0f, 0f, (float)this.width, (float)this.height), 0, 0);
		texture2d.Apply();
		RenderTexture.active = active;
		RenderTexture.ReleaseTemporary(temporary);
	}

	public void Clear()
	{
		RenderTexture temporary = RenderTexture.GetTemporary(this.width, this.height, 16);
		base.GetComponent<Camera>().targetTexture = temporary;
		RenderTexture active = RenderTexture.active;
		RenderTexture.active = temporary;
		int cullingMask = base.GetComponent<Camera>().cullingMask;
		base.GetComponent<Camera>().cullingMask = 0;
		base.GetComponent<Camera>().Render();
		Texture2D texture2D = new Texture2D(this.width, this.height, TextureFormat.ARGB32, this.useMipMap);
		texture2D.ReadPixels(new Rect(0f, 0f, (float)this.width, (float)this.height), 0, 0);
		texture2D.Apply();
		base.GetComponent<Camera>().cullingMask = cullingMask;
		RenderTexture.active = active;
		RenderTexture.ReleaseTemporary(temporary);
	}

	public bool useFullScreen;

	public int width = 256;

	public int height = 256;

	public bool useMipMap;
}
